package com.example.boot6rabbitmqalipay.timer;

import com.alibaba.fastjson.JSONObject;

import com.example.boot6rabbitmqalipay.bean.Message;
import com.example.boot6rabbitmqalipay.dao.MessageMapper;
import com.example.boot6rabbitmqalipay.rabbitmq.RabbitmqSender;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class ScheduledService {

    @Autowired
    MessageMapper messageMapper;

    @Autowired
    RabbitmqSender rabbitmqSender;

    @Scheduled(cron = "0/5 0/1 * * * ?")
    public void scheduled(){
        log.info("=====>>>>>使用cron  {}",System.currentTimeMillis());
        List<Message> messages = messageMapper.queryMessageByState("unconfirm");
        log.info("============messages:" + JSONObject.toJSONString(messages));
        for (Message message : messages) {
            log.info("=======定时器往rabbitmq发送消息:" + JSONObject.toJSONString(message));
            rabbitmqSender.sendMessage("exchange.message", "jack.message.routeKey", message);
        }
    }
}
